package com.ztc.ZtcClassesForSale.service;
import java.text.ParseException;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import com.aqgj.User.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.aqgj.Attachment.bean.Attachment;
import com.aqgj.Attachment.service.AttachmentManager;
import com.ztc.ZtcClasses.bean.ZtcClasses;
import com.ztc.ZtcClasses.dao.ZtcClassesDao;
import com.ztc.ZtcClassesForSale.bean.ZtcClassesForSale;
import com.ztc.ZtcClassesForSale.dao.ZtcClassesForSaleDao;
import com.ztc.ZtcClassesPriceForSale.bean.ZtcClassesPriceForSale;
import com.ztc.ZtcClassesPriceForSale.dao.ZtcClassesPriceForSaleDao;
import com.ztc.ZtcClassesPriceForSale.service.ZtcClassesPriceForSaleManager;
import com.ztc.ZtcClassesSiteForSale.bean.ZtcClassesSiteForSale;
import com.ztc.ZtcClassesSiteForSale.dao.ZtcClassesSiteForSaleDao;
import com.ztc.ZtcClassesSiteForSale.service.ZtcClassesSiteForSaleManager;
import com.ztc.ZtcRouteAttachment.bean.ZtcRouteAttachment;
import com.ztc.ZtcRouteAttachment.dao.ZtcRouteAttachmentDao;
import com.ztc.ZtcTransport.service.ZtcTransportManager;

@Service("ZtcClassesForSale")
public class ZtcClassesForSaleManager {
	@Autowired
	ZtcClassesForSaleDao ztcClassesForSaleDao;
	@Autowired
	ZtcClassesPriceForSaleDao ztcClassesPriceForSaleDao;
	@Autowired
	ZtcClassesSiteForSaleDao ztcClassesSiteForSaleDao;
	@Autowired
	ZtcClassesDao ztcClassesDao;
	@Autowired
	ZtcRouteAttachmentDao ztcRouteAttachmentDao;
	@Autowired
	AttachmentManager attachmentManager;
	@Autowired
	ZtcTransportManager ztcTransportManager;
	@Autowired
	ZtcClassesForSaleManager ztcClassesForSaleManager;
	@Autowired
	ZtcClassesSiteForSaleManager ztcClassesSiteForSaleManager;
	@Autowired
	ZtcClassesPriceForSaleManager ztcClassesPriceForSaleManager;

	public String save(ZtcClassesForSale ztcClassesForSale) {
		return ztcClassesForSaleDao.save(ztcClassesForSale);
	}

	/**
	 * 获取某在线班次所有站点
	 * @param ztcClassesForSale 在线班次对象
	 * @return 站点集合
	 */
	public List<ZtcClassesSiteForSale> getSitesForSale(ZtcClassesForSale ztcClassesForSale) {
		ZtcClassesSiteForSale site = new ZtcClassesSiteForSale();
		site.setFkZtcClassesForSaleId(ztcClassesForSale.getId());
		return ztcClassesSiteForSaleDao.searchInfoEquals(site);
	}

	/**
	 * 获取某在线班次所有价格
	 * @param ztcClassesForSale 在线班次对象
	 * @return 价格集合
	 */
	public List<ZtcClassesPriceForSale> getPricesForSale(ZtcClassesForSale ztcClassesForSale) {
		ZtcClassesPriceForSale price = new ZtcClassesPriceForSale();
		price.setFkClassesForSaleId(ztcClassesForSale.getId());
		return ztcClassesPriceForSaleDao.searchInfoEquals(price);
	}

	/**
	 * 根据在线班次，公司类型，页数模糊查找在线班次，并按在线班次第一个站点发车时间排序
	 * @param classesForSale 在线班次对象
	 * @param transportType 公司类型
	 * @param pageNumber 第几页
	 * @return 在线班次集合
	 */
	public List<ZtcClassesForSale> searchInfoLikes(ZtcClassesForSale classesForSale, String transportType,
												   String pageNumber) {
		List<ZtcClassesForSale> list = ztcClassesForSaleDao.searchInfoLikes(classesForSale, transportType, pageNumber);
		sortList(list);
		return list;
	}

	/**
	 * 根据在线班次，开始日期，结束日期查询在线班
	 * @param classesForSale 在线班次对象
	 * @param beginDate 开始日期
	 * @param endDate 结束日期
	 * @return 在线班次集合
	 */
	public List<ZtcClassesForSale> searchInfoWithDate(ZtcClassesForSale classesForSale, String beginDate,
													  String endDate) {
		List<ZtcClassesForSale> list = ztcClassesForSaleDao.searchInfoEquals(classesForSale, beginDate, endDate);
		return list;
	}
	public List<ZtcClassesForSale> searchInfoWithDateNoSort(ZtcClassesForSale classesForSale, String beginDate,
															String endDate) {
		List<ZtcClassesForSale> list = ztcClassesForSaleDao.searchInfoEquals(classesForSale, beginDate, endDate);
		return list;
	}

	public List<ZtcClassesForSale> searchInfoEqualsNoSort(ZtcClassesForSale classesForSale) {
		List<ZtcClassesForSale> list = ztcClassesForSaleDao.searchInfoEquals(classesForSale, null, null);
		return list;
	}
	public List<ZtcClassesForSale> searchInfoEquals(ZtcClassesForSale classesForSale) {
		List<ZtcClassesForSale> list = ztcClassesForSaleDao.searchInfoEquals(classesForSale, null, null);
		return list;
	}

	/**
	 * 根据在线班次id或公司id或发布用户id或修改用户id删除在线班次
	 * @param id 班次id
	 * @param transportId 公司id
	 * @param publishUserId 发布用户id
	 * @param modifyUserId 修改用户id
	 * @return 操作结果信息：0.失败1.成功
	 */
	public String delete(String id, String transportId, String publishUserId, String modifyUserId) {
		return ztcClassesForSaleDao.delete(id, transportId, publishUserId, modifyUserId);
	}

	public String update(ZtcClassesForSale ztcClassesForSale) {
		return  ztcClassesForSaleDao.update(ztcClassesForSale);
	}

	/**
	 * 停售
	 * @param ztcClassesForSale 在线班次对象
	 * @return 操作结果集合
	 */
	public String stopSell(ZtcClassesForSale ztcClassesForSale, User user, Date now) {
		ZtcClassesPriceForSale ztcPriceForSale = new ZtcClassesPriceForSale();
		ztcPriceForSale.setFkClassesForSaleId(ztcClassesForSale.getId());
		List<ZtcClassesPriceForSale> prices = ztcClassesPriceForSaleDao.searchInfoEquals(ztcPriceForSale);
		if(ztcClassesForSale.getType()==0){
			for (ZtcClassesPriceForSale price : prices) {
				if (price == null || price.getStatus() == 1) {
					continue;
				}
				String seatInfos = price.getSeatInfo();
				if("".equals(seatInfos)||"()".equals(seatInfos)){
					continue;
				}
				String[] seatInfo = seatInfos.split("\\u0029,");
				String newSeatInfo = "";
				for (int i = 0; i < seatInfo.length; i++) {
					String seat = seatInfo[i];
					if ("".equals(seat)) {
						continue;
					}
					int seatIndex = seat.indexOf(")");
					if (seatIndex > 0) {
						seat = seat.substring(1, seatIndex);
					} else {
						seat = seat.substring(1);
					}
					String[] tags = seat.split(",");
					if ("0".equals(tags[1])) {
						newSeatInfo += "(" + tags[0] + ",2," + tags[2] + "," + tags[3] + "),";
					} else {
						newSeatInfo += "(" + seat + "),";
					}
				}
				if (newSeatInfo.length() > 0) {
					newSeatInfo = newSeatInfo.substring(0, newSeatInfo.length() - 1);
				}
				if(!"".equals(newSeatInfo)&&!"()".equals(newSeatInfo)){
					price.setSeatInfo(newSeatInfo);
				}
				price.setStatus(1);
				price.setFkModifyUserId(user.getId());
				price.setModifyTime(now);
				String priceStatus = ztcClassesPriceForSaleDao.update(price);
				if ("0".equals(priceStatus)) {
					return "0";
				}
			}
		}else{
			for (ZtcClassesPriceForSale price : prices) {
				if (price == null || price.getStatus() == 1) {
					continue;
				}
				price.setStatus(1);
				price.setFkModifyUserId(user.getId());
				price.setModifyTime(now);
				String priceStatus = ztcClassesPriceForSaleDao.update(price);
				if ("0".equals(priceStatus)) {
					return "0";
				}
			}
		}
		ztcClassesForSale.setTicketStatus(1);
		String classStatus = ztcClassesForSaleDao.update(ztcClassesForSale);
		if ("0".equals(classStatus)) {
			return "0";
		}
		return "1";
	}

	/**
	 * 起售停售的在线班次
	 * @param ztcClassesForSale 在线班次对象
	 * @return 操作结果信息：0.失败1.成功
	 */
	public String restartToSell(ZtcClassesForSale ztcClassesForSale,User user,Date now) {
		ZtcClassesPriceForSale ztcPriceForSale = new ZtcClassesPriceForSale();
		ztcPriceForSale.setFkClassesForSaleId(ztcClassesForSale.getId());
		List<ZtcClassesPriceForSale> prices = ztcClassesPriceForSaleDao.searchInfoEquals(ztcPriceForSale);
		int priceReSell = 0;
		if(ztcClassesForSale.getType()==0){
			for (ZtcClassesPriceForSale price : prices) {
				if (price == null) {
					continue;
				}
				ZtcClassesSiteForSale site = new ZtcClassesSiteForSale();
				site.setId(price.getFkEndClassesSiteForSaleId());
				ZtcClassesSiteForSale endSite = ztcClassesSiteForSaleDao.searchInfoEquals(site).get(0);
				if (endSite.getDepartureTime().getTime()<System.currentTimeMillis()) {
					continue;
				}
				String seatInfos = price.getSeatInfo();
				if("".equals(seatInfos)||"()".equals(seatInfos)){
					continue;
				}
				String[] seatInfo = seatInfos.split("\\u0029,");
				String newSeatInfo = "";
				int seatReSell = 0;
				for (int i = 0; i < seatInfo.length; i++) {
					String seat = seatInfo[i];
					if ("".equals(seat)) {
						continue;
					}
					int seatIndex = seat.indexOf(")");
					if (seatIndex > 0) {
						seat = seat.substring(1, seatIndex);
					} else {
						seat = seat.substring(1);
					}
					String[] tags = seat.split(",");
					if ("2".equals(tags[1])) {
						seatReSell++;
						newSeatInfo += "(" + tags[0] + ",0," + tags[2] + "," + tags[3] + "),";
					} else {
						newSeatInfo += "(" + seat + "),";
					}
				}
				if (newSeatInfo.length() > 0) {
					newSeatInfo = newSeatInfo.substring(0, newSeatInfo.length() - 1);
				}
				if(!"".equals(newSeatInfo)&&!"()".equals(newSeatInfo)){
					price.setSeatInfo(newSeatInfo);
				}
				if (seatReSell > 0) {
					price.setStatus(0);
					price.setFkModifyUserId(user.getId());
					price.setModifyTime(now);
					String priceStatus = ztcClassesPriceForSaleDao.update(price);
					if ("0".equals(priceStatus)) {
						return "0";
					}
					priceReSell++;
				}
			}
		}else{
			for (ZtcClassesPriceForSale price : prices) {
				if (price == null) {
					continue;
				}
				ZtcClassesSiteForSale site = new ZtcClassesSiteForSale();
				site.setId(price.getFkStartClassesSiteForSaleId());
				ZtcClassesSiteForSale startSite = ztcClassesSiteForSaleDao.searchInfoEquals(site).get(0);
				if (startSite.getStatus() == 1) {
					continue;
				}
				if(price.getRemainSeatNumber()>0){
					price.setStatus(0);
					price.setFkModifyUserId(user.getId());
					price.setModifyTime(now);
					String priceStatus = ztcClassesPriceForSaleDao.update(price);
					if ("0".equals(priceStatus)) {
						return "0";
					}
					priceReSell++;
				}
			}
		}
		if (priceReSell > 0) {
			ztcClassesForSale.setTicketStatus(0);
			String classStatus = ztcClassesForSaleDao.update(ztcClassesForSale);
			if ("0".equals(classStatus)) {
				return "0";
			}
		}
		return "1";
	}

	/**
	 * 根据名称，公司类型，公司id统计在线班次
	 * @param name 名称
	 * @param transportType 公司类型
	 * @param transportId 公司id
	 * @return 在线班次数量
	 */
	public int getCount(String name, String transportType, String transportId) {
		return ztcClassesForSaleDao.getCount(name, transportType, transportId);
	}

	/**
	 * 获取某班次的所有附件
	 * @param ztcClassesForSaleBean 在线班次对象
	 * @return 附件集合
	 */
	public List<ZtcRouteAttachment> getAttachments(ZtcClassesForSale ztcClassesForSaleBean) {
		ZtcClasses ztcClasses = new ZtcClasses();
		ztcClasses.setId(ztcClassesForSaleBean.getFkZtcClassesId());
		ZtcClasses ztcClassesBean = ztcClassesDao.searchInfoEquals(ztcClasses).get(0);
		ZtcRouteAttachment ztcRouteAttachment = new ZtcRouteAttachment();
		ztcRouteAttachment.setFkZtcRouteId(ztcClassesBean.getFkZtcRouteId());
		List<ZtcRouteAttachment> ztcRouteAttachments = ztcRouteAttachmentDao.searchInfoEquals(ztcRouteAttachment);
		for (int i = 0; i < ztcRouteAttachments.size(); i++) {
			ZtcRouteAttachment ztcRouteAttachmentBean = ztcRouteAttachments.get(i);
			if (!"".equals(ztcRouteAttachmentBean.getFkAttachmentId())
					&& ztcRouteAttachmentBean.getFkAttachmentId() != null) {
				Attachment attachment = attachmentManager.get(ztcRouteAttachmentBean.getFkAttachmentId());
				ztcRouteAttachmentBean.setAttachment(attachment);
			}
		}
		return ztcRouteAttachments;
	}

	private void sortList(List<ZtcClassesForSale> list) {
		Collections.sort(list, new Comparator<ZtcClassesForSale>() {
			@Override
			public int compare(ZtcClassesForSale ztcClassesForSaleOne, ZtcClassesForSale ztcClassesForSaleTwo) {
				ZtcClassesSiteForSale oneSite = new ZtcClassesSiteForSale();
				oneSite.setFkZtcClassesForSaleId(ztcClassesForSaleOne.getId());
				oneSite.setSequence(0);
				ZtcClassesSiteForSale siteOne = ztcClassesSiteForSaleDao.searchInfoEquals(oneSite).get(0);
				ZtcClassesSiteForSale twoSite = new ZtcClassesSiteForSale();
				twoSite.setFkZtcClassesForSaleId(ztcClassesForSaleTwo.getId());
				twoSite.setSequence(0);
				ZtcClassesSiteForSale siteTwo = ztcClassesSiteForSaleDao.searchInfoEquals(twoSite).get(0);
				if (siteOne.getDepartureTime().getTime() < siteTwo.getDepartureTime().getTime()) {
					return -1;
				} else if (siteOne.getDepartureTime().getTime() == siteTwo.getDepartureTime().getTime()) {
					return 0;
				} else {
					return 1;
				}
			}
		});
	}

	/**
	 * 站点到站：将所有站点发车时间小于当前时间的站点设为到站
	 * @return 操作结果信息：0.失败1.成功
	 */
	public String siteArriving(){
		List<ZtcClassesSiteForSale> siteList = ztcClassesSiteForSaleDao.searchOverdueSiteForSale();
		for (int i = 0; i < siteList.size(); i++) {
			ZtcClassesSiteForSale currentSiteForSale = siteList.get(i);
			ZtcClassesForSale ztcClassesForSale = new ZtcClassesForSale();
			ztcClassesForSale.setId(currentSiteForSale.getFkZtcClassesForSaleId());
			ZtcClassesForSale currentClassesForSale = ztcClassesForSaleManager.searchInfoEquals(ztcClassesForSale)
					.get(0);
			if(currentClassesForSale.getType()==0){
				if (currentSiteForSale.getSequence() == 0) {
					currentClassesForSale.setStatus(1);
					String result = ztcClassesForSaleDao.update(currentClassesForSale);
					if ("0".equals(result)) {
						return "0";
					}
					currentSiteForSale.setStatus(1);
					String siteStatus = ztcClassesSiteForSaleDao.update(currentSiteForSale);
					if ("0".equals(siteStatus)) {
						return "0";
					}
				} else {
					ZtcClassesSiteForSale ztcClassesSiteForSale = new ZtcClassesSiteForSale();
					ztcClassesSiteForSale.setFkZtcClassesForSaleId(currentSiteForSale.getFkZtcClassesForSaleId());
					List<ZtcClassesPriceForSale> prices = ztcClassesPriceForSaleManager
							.searchByEndSiteId(currentSiteForSale.getFkZtcClassesForSaleId());
					for (ZtcClassesPriceForSale ztcPrice : prices) {
						String seatInfos = ztcPrice.getSeatInfo();
						if("".equals(seatInfos)||"()".equals(seatInfos)){
							continue;
						}
						String[] seatInfo = seatInfos.split("\\u0029,");
						String newSeatInfo = "";
						for (int j = 0; j < seatInfo.length; j++) {
							String seat = seatInfo[j];
							if ("".equals(seat)) {
								continue;
							}
							int seatIndex = seat.indexOf(")");
							if (seatIndex > 0) {
								seat = seat.substring(1, seatIndex);
							} else {
								seat = seat.substring(1);
							}
							String[] tags = seat.split(",");
							if ("0".equals(tags[1])) {
								newSeatInfo += "(" + tags[0] + ",2," + tags[2] + "," + tags[3] + "),";
							} else {
								newSeatInfo += "(" + seat + "),";
							}
						}
						if (newSeatInfo.length() > 0) {
							newSeatInfo = newSeatInfo.substring(0, newSeatInfo.length() - 1);
						}
						if(!"".equals(newSeatInfo)&&!"()".equals(newSeatInfo)){
							ztcPrice.setSeatInfo(newSeatInfo);
						}
						ztcPrice.setStatus(1);
						String priceStatus = ztcClassesPriceForSaleDao.update(ztcPrice);
						if ("0".equals(priceStatus)) {
							return "0";
						}
					}
					currentSiteForSale.setStatus(1);
					String siteStatus = ztcClassesSiteForSaleDao.update(currentSiteForSale);
					if ("0".equals(siteStatus)) {
						return "0";
					}
					List<ZtcClassesSiteForSale> sites = ztcClassesSiteForSaleDao
							.searchInfoEquals(ztcClassesSiteForSale);
					if (currentSiteForSale.getSequence() + 1 == sites.size()) {
						if (currentClassesForSale.getTicketStatus() == 0) {
							currentClassesForSale.setStatus(2);
							currentClassesForSale.setTicketStatus(1);
							String updateResult = ztcClassesForSaleDao.update(currentClassesForSale);
							if ("0".equals(updateResult)) {
								return "0";
							}
							String classesId = currentClassesForSale.getFkZtcClassesId();
							int forSaleCount = ztcClassesForSaleDao.getCount(classesId, null);
							int stopCount = ztcClassesForSaleDao.getCount(classesId, "2");
							if (forSaleCount == stopCount) {
								ZtcClasses ztcClasses = ztcClassesDao.get(classesId);
								ztcClasses.setTicketStatus(1);
								String updateClassesResult = ztcClassesDao.update(ztcClasses);
								if ("0".equals(updateClassesResult)) {
									return "0";
								}
							}
						}
					}
				}
			}else if(currentClassesForSale.getType()==1){
				if(currentSiteForSale.getType()==1){
					currentSiteForSale.setStatus(1);
					String updateSiteForSaleResult=ztcClassesForSaleDao.update(currentClassesForSale);
					if("0".equals(updateSiteForSaleResult)){
						return "0";
					}
					if(currentClassesForSale.getStatus()==0){
						currentClassesForSale.setStatus(1);
					}
					String updateClassesForSaleResult=ztcClassesForSaleDao.update(currentClassesForSale);
					if("0".equals(updateClassesForSaleResult)){
						return "0";
					}
				}/*else if(currentSiteForSale.getType()==2){
					ZtcClassesPriceForSale ztcClassesPriceForSaleExample=new ZtcClassesPriceForSale();
					ztcClassesPriceForSaleExample.setFkEndClassesSiteForSaleId(currentSiteForSale.getId());
					ztcClassesPriceForSaleExample.setFkClassesForSaleId(currentClassesForSale.getId());
					List<ZtcClassesPriceForSale> priceList=ztcClassesPriceForSaleDao.searchInfoEquals(ztcClassesPriceForSaleExample);
					ZtcClassesSiteForSale boardingSiteExample=new ZtcClassesSiteForSale();
					boardingSiteExample.setFkZtcClassesForSaleId(currentClassesForSale.getId());
					boardingSiteExample.setType(2);
					List<ZtcClassesSiteForSale> boardingSites=ztcClassesSiteForSaleManager.searchInfoEquals(boardingSiteExample);
					for(int j=0;j<priceList.size();j++){
						ZtcClassesPriceForSale priceObject=priceList.get(j);
						priceObject.setStatus(1);
						String updatePriceForSaleResult=ztcClassesPriceForSaleDao.update(priceObject);
						if("0".equals(updatePriceForSaleResult)){
							return "0";
						}
					}
					currentSiteForSale.setStatus(1);
					String updateSiteForSaleResult=ztcClassesForSaleDao.update(currentClassesForSale);
					if("0".equals(updateSiteForSaleResult)){
						return "0";
					}
					int count=0;
					for(int j=0;j<boardingSites.size();j++){
						if(boardingSites.get(j).getStatus()==1){
							count++;
						}
					}
					if(count==boardingSites.size()){
						currentClassesForSale.setStatus(2);
					}
					String updateClassesForSaleResult=ztcClassesForSaleDao.update(currentClassesForSale);
					if("0".equals(updateClassesForSaleResult)){
						return "0";
					}
				}*/
			}
		}
		return "1";
	}
}
